package com.py.androidUtils.UI;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.py.androidUtils.UI.adapter.SelectImagesAdapter;
import com.py.androidUtils.UI.view.BelongCategoryView;
import com.py.androidUtils.UI.view.BelongCategoryView.TypeOnClickListener;
import com.py.androidUtils.UI.view.ShowPhotosView;
import com.py.androidUtils.UI.view.ShowPhotosView.PhotosClickListener;
import com.py.androidUtils.base.BaseActivity;
import com.py.androidUtils.utils.FileManagerUtils;
import com.py.androidUtils.utils.LogUtils;
import com.py.androidUtils.utils.ToastUtil;
import com.py.androidUtils.view.NoScrollGridView;
import com.readyidu.app.R;

/**
 * @说明 图片发布
 * @作者 LY
 * @文件 PicturesReleasedFragment.java
 * @时间 2015年7月18日 下午4:25:55
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public class PicturesReleasedActivity extends BaseActivity implements OnClickListener {
	private EditText info;
	private ImageView addImages;
	private TextView position;
	private TextView belongCategory;
	private RelativeLayout rlWeCanSee;
	private TextView canSeeType;
	private TextView invitationSee;
	private CheckBox circleFriends;
	private static String typeName = "";
	private String imgUrl = "";
	private ShowPhotosView showPhotosView;
	private NoScrollGridView imagsGrid;
	private SelectImagesAdapter selectImagesAdapter;

	private String address = "";
	private String canSeeTypeName = "";
	private List<String> listImg = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_pictures_released);
		info = (EditText) this.findViewById(R.id.info);
		addImages = (ImageView) this.findViewById(R.id.addImages);
		position = (TextView) this.findViewById(R.id.position);
		belongCategory = (TextView) this.findViewById(R.id.belongCategory);
		rlWeCanSee = (RelativeLayout) this.findViewById(R.id.rlWeCanSee);
		canSeeType = (TextView) this.findViewById(R.id.canSeeType);
		invitationSee = (TextView) this.findViewById(R.id.invitationSee);
		circleFriends = (CheckBox) this.findViewById(R.id.circleFriends);
		imagsGrid = (NoScrollGridView) this.findViewById(R.id.imagsGrid);
		selectImagesAdapter = new SelectImagesAdapter(getApplicationContext());
		selectImagesAdapter.addObject("");
		imagsGrid.setAdapter(selectImagesAdapter);

		getTitleActionBar().setTitleBar("图片发布");
		getTitleActionBar().setImgLeft(R.drawable.icon_back, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finishactivity(PicturesReleasedActivity.this);
			}
		});
		getTitleActionBar().setTvRight1("发布", new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isCheckData()) {
					ToastUtil.showToast(getApplicationContext(), "输入内容：" + info.getText().toString() + "\n" + "图片：" + listImg.toString() + "\n" + "地址：" + address + "\n" + "类别：" + typeName + "\n" + "可以看:" + canSeeTypeName + "\n" + "是否分享：" + (circleFriends.isChecked() ? "分享" : "不分享"));
				}
			}
		});

		position.setOnClickListener(this);
		belongCategory.setOnClickListener(this);
		rlWeCanSee.setOnClickListener(this);
		invitationSee.setOnClickListener(this);
		addImages.setOnClickListener(this);

		imagsGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (selectImagesAdapter.getItem(position) != null && ((String) selectImagesAdapter.getItem(position)).trim().length() > 0) {

				} else {
					if (listImg.size() <= 8) {
						addImages();
					}
				}
			}
		});
	}

	private void addImages() {
		showPhotosView = new ShowPhotosView(PicturesReleasedActivity.this);
		showPhotosView.showView(addImages);
		showPhotosView.setPhotosClickListener(new PhotosClickListener() {
			@Override
			public void selectImages(int requestCode) {
			}

			@Override
			public void letCamera(String imagesPath, int requestCode) {
				imgUrl = imagesPath;
			}
		});
	}

	private boolean isCheckData() {
		if (info.getText().toString().trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请输入发表内容");
			return false;
		}
		if (address.trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择所在位置");
			return false;
		}
		if (typeName.trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择所属类别");
			return false;
		}
		if (canSeeTypeName.trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择谁可以看");
			return false;
		}
		if (listImg.size() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择照片");
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.position:
			launcherActivityForResult(PicturesReleasedActivity.this, PositionActivity.class, null, 1);
			break;
		case R.id.belongCategory:
			BelongCategoryView belongCategoryView = new BelongCategoryView(PicturesReleasedActivity.this);
			belongCategoryView.showDiaolge();
			belongCategoryView.setTypeOnClickListener(new TypeOnClickListener() {
				@Override
				public void getType(String type) {
					typeName = type;
					LogUtils.i(typeName);
				}
			});
			break;
		case R.id.rlWeCanSee:
			launcherActivityForResult(PicturesReleasedActivity.this, WeCanSeeActivity.class, null, 3);
			break;
		case R.id.addImages:
			break;
		case R.id.invitationSee:
			ToastUtil.showToast(getApplicationContext(), "邀请谁看");
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg1 == RESULT_OK) {
			switch (arg0) {
			case 1:
				LogUtils.i(arg2.getStringExtra("name"));
				address = arg2.getStringExtra("name");
				break;
			case 2:
				LogUtils.i(arg2.getStringExtra("typeName"));
				typeName = arg2.getStringExtra("typeName");
				break;
			case 3:
				LogUtils.i(arg2.getStringExtra("type"));
				canSeeType.setText(arg2.getStringExtra("type"));
				canSeeTypeName = canSeeType.getText().toString();
				break;
			case ShowPhotosView.IMAGES_ACTION_SELECT_IMAGES:// 选择照片
				imgUrl = FileManagerUtils.getInstance().getPath(PicturesReleasedActivity.this, arg2.getData());
				LogUtils.i("path" + imgUrl);
				listImg.add(imgUrl);
				selectImagesAdapter.clear();
				selectImagesAdapter.setList(listImg);
				if (listImg.size() < 8) {
					selectImagesAdapter.addObject("");
				}
				selectImagesAdapter.notifyDataSetChanged();
				break;
			case ShowPhotosView.IMAGES_ACTION_CAMERA:// 拍照
				LogUtils.i("path" + imgUrl);
				listImg.add(imgUrl);
				selectImagesAdapter.clear();
				selectImagesAdapter.setList(listImg);
				if (listImg.size() < 8) {
					selectImagesAdapter.addObject("");
				}
				selectImagesAdapter.notifyDataSetChanged();
				break;
			}
		}
	}
}
